package com.jboltai.event.ai.aitext;

import cn.hutool.core.util.StrUtil;
import com.jboltai.capability.message.AIMessage;
import com.jboltai.event.Event;
import com.jboltai.event.EventType;
import com.jboltai.event.ThinkHandler;
import com.jboltai.event.ThinkStatus;
import com.jboltai.event.ai.IAIEvent;
import com.jboltai.event.ai.aichat.AIChatEvent;
import com.jboltai.event.handler.EventHandler;
import com.jboltai.resource.ai.AIModel;
import com.jboltai.util.share.AIEventThreadShareData;

import java.util.List;
import java.util.Map;

public abstract class AITextEvent<T extends Event, ResultType, HandlerType extends EventHandler> extends Event<T, ResultType,HandlerType> implements IAIEvent<T, ResultType,HandlerType> {
    protected AITextEvent(EventType type) {
        super(type);
        this.aiChatEvent = new AIChatEvent();
    }

    @Override
    public T setThreadShareData(Map<String, Object> data) {
        super.setThreadShareData(data);
        this.aiChatEvent.setThreadShareData(data);
        return (T) this;
    }

    @Override
    public T setThreadShareData(AIEventThreadShareData threadShareData) {
        super.setThreadShareData(threadShareData);
        this.aiChatEvent.setThreadShareData(threadShareData);
        return (T) this;
    }

    /**
     * 最终生效的prompt
     */
    protected String prompt;

    /**
     * 用户的原始prompt
     */
    protected String oriPrompt;

    /**
     * ai text事件都需要先通过一个ai chat事件获取大模型的结果，再做后续处理。
     * 该成员变量记录的就是依赖的aichat事件
     */
    protected AIChatEvent aiChatEvent;

    protected ThinkHandler<T> thinkHandler;

    private ThinkStatus thinkStatus;

    public AIChatEvent getAiChatEvent() {
        return aiChatEvent;
    }

    /**
     * 获取大模型的深度思考结果
     * @return
     */
    public String getThinkResult() {
        return aiChatEvent.getThinkResult();
    }

    /**
     * 获取大模型的深度思考耗时
     * @return
     */
    public int getThinkDuration() {
        return aiChatEvent.getThinkDuration();
    }


    @Override
    protected void innerCancelHandle() {
        this.aiChatEvent.cancel();
    }

    /**
     * AI大模型深度思考的回调，目前仅对DeepSeek生效
     * @param thinkHandler
     * @return
     */
    public T onThinking(ThinkHandler<T> thinkHandler) {
        this.thinkHandler = thinkHandler;
        return (T) this;
    }

    @Override
    public Map<String, Object> getParams() {
        return aiChatEvent.getParams();
    }

    @Override
    public T setParams(Map<String, Object> params) {
        aiChatEvent.setParams(params);
        return (T) this;
    }

    @Override
    public T addParam(String key, Object value) {
        aiChatEvent.addParam(key, value);
        return (T) this;
    }

    @Override
    public <V> V getParam(String key) {
        return aiChatEvent.getParam(key);
    }

    @Override
    public T bindAIResource(String resourceId) {
        aiChatEvent.bindAIResource(resourceId);
        return (T) this;
    }

    @Override
    public T setTemperature(Double temperature) {
        aiChatEvent.setTemperature(temperature);
        return (T) this;
    }

    @Override
    public T setTopP(Double topP) {
        aiChatEvent.setTopP(topP);
        return (T) this;
    }

    @Override
    public T setTopK(Integer topK) {
        aiChatEvent.setTopK(topK);
        return (T) this;
    }

    @Override
    public String getAIResource() {
        return aiChatEvent.getAIResource();
    }

    @Override
    public String getModelName() {
        return aiChatEvent.getModelName();
    }

    @Override
    public AIModel getModel() {
        return aiChatEvent.getModel();
    }

    @Override
    public Double getTemperature() {
        return aiChatEvent.getTemperature();
    }

    @Override
    public Double getTopP() {
        return aiChatEvent.getTopP();
    }

    @Override
    public Integer getTopK() {
        return aiChatEvent.getTopK();
    }

    @Override
    public List<AIMessage> getMessages() {
        return aiChatEvent.getMessages();
    }

    @Override
    public T contextMessages(List<AIMessage> contextMessages) {
        aiChatEvent.contextMessages(contextMessages);
        return (T) this;
    }

    @Override
    public T systemMessage(String content) {
        aiChatEvent.systemMessage(content);
        return (T) this;
    }

    @Override
    public T userMessage(String content) {
        aiChatEvent.userMessage(content);
        return (T) this;
    }

    @Override
    public T userMessage(String content, String... imgUrls) {
        aiChatEvent.userMessage(content, imgUrls);
        return (T) this;
    }

    @Override
    public T assistantMessage(String content) {
        aiChatEvent.assistantMessage(content);
        return (T) this;
    }

    @Override
    public T setModel(AIModel model) {
        aiChatEvent.setModel(model);
        return (T) this;
    }

    @Override
    public T setModelName(String modelName) {
        aiChatEvent.setModelName(modelName);
        return (T) this;
    }

    public T prompt(String prompt) {
        this.oriPrompt = prompt;
        return (T) this;
    }

    public String getPrompt() {
        return this.oriPrompt;
    }

    @Override
    public T prompt(String template, Object... values) {
        this.oriPrompt = StrUtil.format(template, values);
        return (T) this;
    }

    @Override
    public boolean isTextInput() {
        return aiChatEvent.isTextInput();
    }

    @Override
    public T imgs(String... imgUrls) {
        aiChatEvent.imgs(imgUrls);
        return (T) this;
    }

    @Override
    public void setTextInput(boolean textInput) {
        aiChatEvent.setTextInput(textInput);
    }

    @Override
    public boolean isImageInput() {
        return aiChatEvent.isImageInput();
    }

    @Override
    public void setImageInput(boolean imageInput) {
        aiChatEvent.setImageInput(imageInput);
    }

    @Override
    public boolean isAudioInput() {
        return aiChatEvent.isAudioInput();
    }

    @Override
    public void setAudioInput(boolean audioInput) {
        aiChatEvent.setAudioInput(audioInput);
    }


    @Override
    public boolean isVideoInput() {
        return aiChatEvent.isVideoInput();
    }

    @Override
    public void setVideoInput(boolean videoInput) {
        aiChatEvent.setVideoInput(videoInput);
    }

    public int getMaxTokens() {
        return aiChatEvent.getMaxTokens();
    }

    public T setMaxTokens(int maxTokens) {
        this.aiChatEvent.setMaxTokens(maxTokens);
        return (T) this;
    }

    public ThinkHandler<T> getThinkHandler() {
        return thinkHandler;
    }

    public void setThinkHandler(ThinkHandler<T> thinkHandler) {
        this.thinkHandler = thinkHandler;
    }

    public ThinkStatus getThinkStatus() {
        return thinkStatus;
    }

    public void setThinkStatus(ThinkStatus thinkStatus) {
        this.thinkStatus = thinkStatus;
    }

    public T setThinkingEnable(boolean thinkingEnable) {
        this.aiChatEvent.setThinkingEnable(thinkingEnable);
        return (T)this;
    }

    public boolean isThinkingEnable() {
        return this.aiChatEvent.isThinkingEnable();
    }


    public T setThinkingBudget(Integer thinkingBudget) {
        this.aiChatEvent.setThinkingBudget(thinkingBudget);
        return (T)this;
    }

    public Integer getThinkingBudget() {
        return this.aiChatEvent.getThinkingBudget();
    }

}
